/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 ******************************************************************************/
package com.kapilvirenahuja.eamsteps.form.impl;

import java.util.HashMap;
import java.util.Map;

import com.kapilvirenahuja.eamsteps.form.IDataForm;

/**
 * This <code>abstract<code> implementation of {@link IDataForm} provides common functionalities:
 * 
 * <ol>
 * <li>A {@link Map} to store the data for data tables </li>
 * <li>A {@link Map} to store the class types representing data tables</li>
 * <li>Method to add data to the maps</li>
 * <li>Method to retreive data from the maps</li>
 * </ol>
 * 
 * @author Kapil Viren Ahuja
 * 
 */
public abstract class BaseDataForm implements IDataForm
{

    protected Map<String, String>              objectTypes = new HashMap<String, String>();
    protected Map<String, Map<String, Object>> dataTable   = new HashMap<String, Map<String, Object>>();
    protected Map<String, Map<String, Object>> responseMap = new HashMap<String, Map<String, Object>>();


    /**
     * @see IDataForm#addData(String, String, Object)
     * 
     */
    public void addData(String dataTableName, String index, Object object)
    {
        // Fetch the Map with data for the specific dataTable
        Map<String, Object> data = this.dataTable.get(dataTableName);

        // Check if the child Map has been initialized or not. First time this should be null.
        if (data == null)
        {
            data = new HashMap<String, Object>();
        }

        // Place the data into the child map.
        data.put(index, object);

        // update the map back into the master map.
        this.dataTable.put(dataTableName, data);
    }


    /**
     * @see IDataForm#getData(String, Integer)
     * 
     */
    public Object getData(String dataTableName, Integer index)
    {
        return dataTable.get(dataTableName).get("Row " + index.toString());
    }


    /**
     * @see IDataForm#addData(String, String, Object)
     * 
     */
    public void addResponse(String dataTableName, String index, Object object)
    {
        // Fetch the Map with data for the specific dataTable
        Map<String, Object> data = this.responseMap.get(dataTableName);

        // Check if the child Map has been initialized or not. First time this should be null.
        if (data == null)
        {
            data = new HashMap<String, Object>();
        }

        // Place the data into the child map.
        data.put(index, object);

        // update the map back into the master map.
        this.responseMap.put(dataTableName, data);
    }


    /**
     * @see IDataForm#getData(String, Integer)
     * 
     */
    public Object getResponse(String dataTableName, Integer index)
    {
        return responseMap.get(dataTableName).get("Row " + index.toString());
    }


    /**
     * @see IDataForm#getObjectType(String)
     * 
     */
    public String getObjectType(String dataTableName)
    {
        return this.objectTypes.get(dataTableName);
    }
}
